package com.xoa.service.inv;

import com.xoa.dao.inv.InvWarehouseMapper;
import com.xoa.model.inv.InvWarehouse;
import com.xoa.util.ToJson;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Service
public class InvWarehouseService {

    @Resource
    private InvWarehouseMapper invWarehouseMapper;

    public ToJson<InvWarehouse> insertInvHouse(HttpServletRequest request, InvWarehouse invWarehouse) {
        ToJson<InvWarehouse> toJson =new ToJson<InvWarehouse>(1,"err");
        try {
            int newWarehouseNo = 00001;
            String warehouseNo = "";
            List<InvWarehouse> list = invWarehouseMapper.selectInvInfo1();
            if(list.size()>0&&list!=null){
                if(list.get(0).getWarehouseNo()!=null&& !list.get(0).getWarehouseNo().isEmpty()){
                    String str = list.get(0).getWarehouseNo().substring(2);
                    int newNo = Integer.parseInt(str) + 1;
                    warehouseNo = "CK"+String.format("%05d", newNo);
                }
                invWarehouse.setWarehouseNo(warehouseNo);
            }else {
                String dtr = String.format("%05d", newWarehouseNo);
                warehouseNo ="CK"+ dtr;
                invWarehouse.setWarehouseNo(warehouseNo);
            }
            int count=invWarehouseMapper.insertSelective(invWarehouse);
            if(count>0){
                toJson.setObject(invWarehouse);
                toJson.setMsg("ok");
                toJson.setFlag(0);
            }

        } catch (Exception e) {
            e.printStackTrace();
            toJson.setFlag(1);
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    public ToJson<InvWarehouse> updateInvHouse(HttpServletRequest request, InvWarehouse invWarehouse) {
        ToJson<InvWarehouse> toJson =new ToJson<InvWarehouse>(1,"err");
        try {
            int count=invWarehouseMapper.updateByPrimaryKeySelective(invWarehouse);
            if(count>0){
                toJson.setObject(invWarehouse);
                toJson.setMsg("ok");
                toJson.setFlag(0);
            }

        } catch (Exception e) {
            e.printStackTrace();
            toJson.setFlag(1);
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    public ToJson<InvWarehouse> selectInvById(HttpServletRequest request, Integer warehouseId) {
        ToJson<InvWarehouse> toJson =new ToJson<InvWarehouse>(1,"err");
        try {
            InvWarehouse invWarehouse=invWarehouseMapper.selectByPrimaryKey(warehouseId);
            if(invWarehouse!=null){
                toJson.setObject(invWarehouse);
                toJson.setMsg("ok");
                toJson.setFlag(0);
            }

        } catch (Exception e) {
            e.printStackTrace();
            toJson.setFlag(1);
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    public ToJson<InvWarehouse> deleteInvById(HttpServletRequest request, Integer warehouseId) {
        ToJson<InvWarehouse> toJson =new ToJson<InvWarehouse>(1,"err");
        try {
            invWarehouseMapper.deleteByPrimaryKey(warehouseId);
            toJson.setMsg("ok");
            toJson.setFlag(0);
        } catch (Exception e) {
            e.printStackTrace();
            toJson.setFlag(1);
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    public ToJson<InvWarehouse> selectInvInfo(HttpServletRequest request, InvWarehouse invWarehouse) {
        ToJson<InvWarehouse> toJson =new ToJson<InvWarehouse>(1,"err");
        try {
            List<InvWarehouse> list=invWarehouseMapper.selectInvInfo(invWarehouse);
            if(list!=null&&list.size()>0){
                toJson.setObj(list);
                toJson.setMsg("ok");
                toJson.setFlag(0);
            }

        } catch (Exception e) {
            e.printStackTrace();
            toJson.setFlag(1);
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }
}
